//index.js
//获取应用实例

Page({
  data:{
    name:"",
    list:[
      { name: "CSS", complete:false},
      { name: "HTML", complete: true },
      { name: "JAVASCRIPT", complete: false }
    ],
    all:false,
    leftCount:2
  },
  addHandle:function(){
    if(!this.data.name){return}
    var todos = this.data.list;
    todos.push(
      {name:this.data.name,complete:false}
    )
    this.setData({
      list: todos,
      name:"",
      leftCount:this.data.leftCount+1
    })
  },
  valHandle:function(e){
    var value = e.detail.value;
    this.setData({
      name:value
    })
  },
  handleDelete:function(e){
    var i = e.currentTarget.dataset.index;
    var item=this.data.list;
    var item =item.splice(i, 1)[0];
    var leftCount = this.data.leftCount - (item.complete ? 0 : 1)
    this.setData({
      list: this.data.list,
      leftCount: leftCount
    })
  },
  toggleHandle:function(e){
    var i=e.currentTarget.dataset.index;
    var item=this.data.list[i];
    item.complete=!item.complete;
    var leftCount=this.data.leftCount+(item.complete ? -1:1)
    this.setData({
      list:this.data.list,
      leftCount:leftCount
    })
  },
  allHeadle:function(){
    var item=this.data.list;
    var all=this.data.all;
    for (var i = 0; i < item.length; i++) {
      if (item[i].complete == false){
        all=true;
      }
      item[i].complete = all
    }
    var leftCount = all ? 0 : this.data.list.length;
    this.setData({
      list:item,
      leftCount: leftCount
    })
    // this.data.all=!this.data.all;
    // var list=this.data.list;
    // var that =this;
    // list.forEach(function(item){
    //   item.complete=that.data.all;
    // })
    // var leftCount=this.data.all? 0:this.data.list.length;
    // this.setData({
    //   list:list,
    //   leftCount:leftCount
    // })
  },
  clearHandle:function(){
    var todos = this.data.list.filter(function(item){
        return !item.complete
    })
    this.setData({
      list:todos
    })
  }
})
